

#include <iostream>

using namespace std;

int longestPalSubstr(string str)
{
	int len = str.length();
	int i, maxLen=1, start=0;
	int low, high;

	for(i=1;i<len;i++)
	{
		//处理长度为偶数的情况
		low = i-1;
		high = i;
		while(low>=0 && high<len && str[low]==str[high])
		{
			if(maxLen<high-low+1)
			{
				start = low;
				maxLen = high-low+1;
			}
			low--;
			high++;
		}
		//处理长度为奇数的情况
		low=i-1;
		high=i+1;
		while(low>=0 && high<len && str[low]==str[high])
		{	
			if(maxLen<high-low+1)
			{
				start = low;
				maxLen = high-low+1;
			}
			low--;
			high++;
		}
	}

	cout<<"最长的回文子串为："<<endl;
	for(i=start; i<start+maxLen; i++)
	{
		cout<<str[i];
	}
	cout<<endl;
	cout<<"长度为："<<maxLen<<endl<<endl;
	return maxLen;
}

int main()
{
	string str = "abcbaaa";
	longestPalSubstr(str);

	return 0;
}

